//app.js
var http = require("./utils/util.js");
const app = getApp()
App({
  netWorkData:{
    result:{code:-1,msg:"请求失败"} ,
    isreq:false, 
  },
  onLoad:function(){
    console.log("App onLoad")

  },
  onLaunch: function () {
    var that = this;
    console.log("App OnLaunch")
    wx.getUserInfo({
      success: function (user) {
        console.log(user)
        // this.setglobalData({
        //   userInfo :user.userInfo
        // })
        that.globalData.userInfo = user.userInfo
      }
    })
    console.log("获取token之前")

    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)


    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: [],
    code:'',
    // localApi:'https://192.168.0.38/api/wxSDK', 
    localApi:"https://www.hifun.cc/api/wxSDK",
  },
  // 登录
  login:function(){
    var that = this;
    console.log("执行login")
    wx.login({
      success: res => {
        console.log(res)
        console.log(that.globalData.localApi)
        that.globalData.code = res.code;
        var user;
        wx.getUserInfo({
          success:function(user){ 
            user = user;
            that.globalData.userInfo = user.userInfo;
            wx.request({
              url:that.globalData.localApi+"/user/getCode",
              method:"get",
              data:{
                code:res.code,
                userInfo:user.userInfo,
              },
              success:function(res){
                console.log(res)
                wx.setStorage({
                  key: 'token',
                  data: res.data.data.token,
                })
                that.checksession();
              }
            })
          }
        })    
        console.log(res)
        console.log(res.code);
      }
    })
  },
  // 上传语音
  receiveVoice: function (silk) {
    var that = this;
   
    return voiceReasult;
  },
  showInfo:function(content){
    wx.showModal({
      title:"提示信息",
      content:content,
      showCancel:false,
      success:function(res){

      }
    })
  },
  checksession:function(){
    var that = this;
    wx.checkSession({
      success:function(res){
        console.log(res)
        that.netWorkData.isreq = true
      },
      fail:function(res){
        console.log("获得session失败")
        that.netWorkData.isreq = false;
      }
    }) 

  },
  showToast:function(title){
    wx.showToast({
      title: title,
    })
  },
  timestamp: function () {
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    return (timestamp)
  },
  sign: function (url, token, timestamp) {
    var sign = url.substring(url.search(/\/api\//g)).replace(/\//g, '/') + token + timestamp;
    return sign;
  },
  // 判断是否位汉字
  checkChinese:function(val){
    var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
    var str = reg.test(val);
    return str
  }
})